/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import algos.AlgoInference;
import graph.LoadGraph;
import ihm.JGraphAdapterDemo;
import javax.swing.JFrame;
import org.jgrapht.UndirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleGraph;
import view.View;

/**
 *
 * @author Arnaud
 */
public class Controller {
    
    UndirectedGraph<Object, DefaultEdge> g;
    LoadGraph loader;
    AlgoInference algoLauncher;
    View view;
    int algoChoice;
    boolean algoSpecialite;
    Long user;

    public Controller() {
        
        g = new SimpleGraph<Object, DefaultEdge>(DefaultEdge.class);
        loader = new LoadGraph(g); 
        
        algoChoice = 0;
        algoSpecialite = false;
    }

    public void setView(View view) {
        this.view = view;
        algoLauncher = new AlgoInference(g, view);
    }
    
    public void addGraphFrame()
    {
         JGraphAdapterDemo applet = new JGraphAdapterDemo();
        
        applet.init();
 
        //JFrame frame = new JFrame();
        view.getInternalFrameGraph().getContentPane().add(applet);
        view.getInternalFrameGraph().setTitle("JGraphT Adapter to JGraph Demo");
        view.getInternalFrameGraph().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        view.getInternalFrameGraph().pack();
        view.getInternalFrameGraph().setVisible(true);
    }
    
    
    public void init()
    {
        loader.fillGraph();
        
         /*
        Long l  = new Long("100003256678055");
        algoLauncher.algoAGG(loader.getHashmapUsers().get(l));*/
    }
    
    public void controllerChooseUser(String u)
    {
        user  = new Long(u);
        System.out.println("User = " + user);
        
        if(loader.getHashmapUsers().containsKey(user))
        {
            view.displayUserProfil(loader.getHashmapUsers().get(user).toString());
        }
        else {
            view.displayUserProfil("Ce profil n'existe pas !");
        }
    }
    
    public void controllerChooseAlgo(int c)
    {
        algoChoice = c;
        
        System.out.println("algoChoice = " + algoChoice);
    }
    
    public void controllerChooseOption(boolean o)
    {
        algoSpecialite = o;
        
        System.out.println("Option = " + algoSpecialite);
    }
    
    public void controllerLaunchAlgo()
    {
        //Long user  = new Long("100003256678055");
        //Long user  = new Long(view.getTextFieldUser().getText());
        
        if(loader.getHashmapUsers().containsKey(user))
        {
            switch(algoChoice)
            {
                case 0: 
                    if(!algoSpecialite)
                    {
                        algoLauncher.BasicForSchool(loader.getHashmapUsers().get(user));       
                    }
                    else {
                        algoLauncher.BasicForSpecialite(loader.getHashmapUsers().get(user));
                    }

                    break;

                case 1:
                    if(!algoSpecialite)
                    {
                        algoLauncher.AGGForSchool(loader.getHashmapUsers().get(user));
                    }
                    else {
                        algoLauncher.AGGForSpecialite(loader.getHashmapUsers().get(user));
                    }
                    break;

                case 2:
                    algoLauncher.CliqueForSchool(loader.getHashmapUsers().get(user));
                    break;
            }
        }
        else view.displayAlgoAnswer("Id utilisateur inexistant dans la base de données !");
    }
}
